home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DOS.SWG / 0003_Re: DOS INI File unit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  5.9 KB  |  221 lines

  1. {
  2.  INI File Reading Unit--
  3.   THis UNIT will allow you to use *.INI files for config instead of a
  4.   full blown setup program, it will read ALMOST ALL INI files, except
  5.   for ones such as CGA.INI.
  6.   I'm sure no one will have trouble with it, it is pretty straight
  7.   forward (even a C/C++ programmer could understand it <g>)
  8.  
  9.  NOTE FROM AUTHOR:
  10.   Well, I'm sure I bugged some people on the FIDOs about stupid errors
  11.   while making this, as it turns out, I have finished my until already
  12.   after just getting rid of the last known bug.
  13.  
  14.   I am releasing this as public domain, if you find it usefull, I would
  15.   appreciate credit. And please, if you make changes, send me a copy.
  16.  
  17.  USE: (An example is included at the end)
  18.   1) Change the constant MAXINI to the maxium amount of variables.
  19.   2) Declare a variable of INI_TYP.
  20.   3) Run INIVALS at the start of your program
  21.   4) Assign the ITEMSTR value to the keyword to look for
  22.      (ie.. if you wanted to associate ANYVAR[1].ITEMSTR := 'HERE',
  23.       then if this is found in the file "HERE=THERE", the return
  24.       result of ANYVAR[1].RESULT will be "THERE")
  25.   5) Run READINI on the INI file.
  26.   6) Use the values returned in RESULT for config.
  27.  
  28. FYI.. Case/Spaces/Tabs do not matter NO MATTER WHERE THEY ARE!
  29.  
  30. BTW.. I cut this code from a program of mine where I originally created it,
  31.       if it doesnt work tell me and I will add whatever I forgot. So far
  32.       it seems to have no obvious problems.
  33.  
  34. SWAG use is permitted.
  35.  
  36. ONE FINAL NOTE:
  37.  I blame all bugs/spelling errors/etc.. on my word processor.
  38.  You use this program at your own risk, I will do accept any liability
  39.  for ANY problems whatsoever.
  40.  
  41. No animals were harmed in the making of this program.
  42. }
  43.  
  44.  
  45.  
  46. {$IFDEF DEBUG}
  47. {$A+,B-,D+,F-,G-,I-,K-,L-,N-,E-,P-,Q+,R+,S+,T-,V-,W-,X+,Y-}
  48. {$ELSE}
  49. {$A+,B-,D-,F-,G-,I-,K-,L-,N-,E-,P-,Q-,R-,S-,T-,V-,W-,X+,Y-}
  50. {$ENDIF}
  51.  
  52. UNIT INIT;  { see test program below }
  53.  
  54. Interface
  55.  
  56. Type
  57.       Str12  = String[12];
  58.       Str26  = String[26];
  59.       Str35  = String[35];
  60.       Str75  = String[75];
  61.       Str127 = String[127];
  62.  
  63.  Const
  64.     MaxIni = 5; {Change this to whatever}
  65.     CommentSet : Set Of Char = ['[','!','#','/','>'];
  66.     INI_FNotFound = $02;  {Returned by READINI}
  67.     INI_FIOError  = $01;  {""}
  68.     INI_FOk       = $00;  {""}
  69.  
  70.  Type
  71.       INI_REC = Record
  72.                  KEY : Str35; {keyword}
  73.                  Result  : Str35; {Found after: ItemStr,'=',Result}
  74.                  Found   : Boolean; {Found yet?}
  75.                 End;
  76.  
  77.       INI_TYP = Array[1..MaxIni] of INI_REC;
  78.  
  79. Procedure InitVals(Var a999 : INI_TYP);
  80. Function ReadIni(F:Str75;var InIv : Ini_Typ):Byte;
  81. Function _S3(Base : String;Var S1,S2 : String):Byte;
  82. Function EraseChar2(Ch:Char;St:String):String;
  83. Function UpStr(const s:string):string;
  84.  
  85. Implementation
  86.  
  87.  Procedure InitVals(Var A999 : INI_TYP);
  88.  Var W:Word;
  89.   Begin
  90.   For W := 1 to MaxIni do A999[W].Found := False;
  91.   End;
  92.  
  93. Function EraseChar2(Ch:Char;St:String):String;
  94.  Var NB:Byte;
  95.  Begin
  96.   For NB := 1 to length(St) do If St[Nb] = CH then Delete(St,Nb,1);
  97.   EraseChar2 := St;
  98.  End;
  99.  
  100. {Function EraseChar(Ch:Char;St:String):String;
  101. Begin
  102.  While Copy(St, 1, 1) = CH do
  103.  Delete(St, 1, 1);
  104.  While Copy(St, Length(St), 1) = CH do
  105.  Delete(St, Length(St), 1);
  106.  EraseCHar := St;
  107. End;}
  108.  
  109. Function _S3(Base : String;Var S1,S2 : String):Byte;
  110.  var B,B2:Byte;
  111.  Begin
  112.   _S3 := 0;
  113.   B := Pos('=',Base);
  114.   If B > 1 then
  115.    Begin
  116.     S1 := Copy(Base,1,B-1);
  117.     S2 := Copy(Base,B+1,Length(Base));
  118.     S1 := EraseChar2(' ',S1);
  119.    End Else _S3 := 1;
  120.  End;
  121.  
  122. Function ReadIni(F:Str75;var InIv : Ini_Typ):Byte;
  123.   Var INIFILE:Text;
  124.       TempStr : Str127;
  125.       S1,S2 : Str35;
  126.       W1 : Word;
  127.   Begin
  128.    Assign(INIFILE, F);
  129.    Reset(INIFILE);
  130.    READINI := 0;
  131.    IF IOresult <> 0 then
  132.     Begin
  133.      ReadInI := INI_FNotFound;
  134.      Exit;
  135.     End;
  136.   While not EOF(INIFILE) do
  137.     Begin
  138.      Readln(INIFILE, TempStr); {Load String}
  139.      If length(TempStr) > 3 then {Min: A=A}
  140.       Begin
  141.        TempStr := UpStr(TempStr); {Make it caps}
  142.        TempStr := EraseChar2(' ',TempStr); {Get rid of spaces}
  143.        TempStr := EraseChar2(#9,TempStr); {Get rid of tabs}
  144.        If not (TempStr[1] in CommentSet) then {Not a comment?}
  145.        If _S3(TempStr, S1, S2) = 0 then {Is it a valid param?}
  146.        For W1 := 1 to MaxIni do
  147.          Begin{Search all INI variables}
  148.           If not INIV[W1].Found then {has not been checked out}
  149.           If UpStr(INIV[W1].Key) = S1 then {Do they match?}
  150.            Begin
  151.             INIV[W1].Result := S2;
  152.             INIV[W1].Found := True;
  153.             W1 := MaxINI; {ENd search}
  154.           End;{Begin If ItemStr = S1}
  155.        End;{For W1 to}
  156.      End;{If Length > 3}
  157.   End;{While not EOF}
  158.    Close(INIFILE);
  159.   End;
  160.  
  161. Function UpStr(const s:string):string; assembler; {Upper Case String}
  162. {This is the only code that is not mine...}
  163.   asm
  164.     push ds
  165.     lds  si,s
  166.     les  di,@result
  167.     lodsb            { load and store length of string }
  168.     stosb
  169.     xor  ch,ch
  170.     mov  cl,al
  171.     jcxz @empty      { FIX for null length string }
  172.   @upperLoop:
  173.     lodsb
  174.     cmp  al,'a'
  175.     jb   @cont
  176.     cmp  al,'z'
  177.     ja   @cont
  178.     sub  al,' '
  179.   @cont:
  180.     stosb
  181.     loop @UpperLoop
  182.   @empty:
  183.     pop  ds
  184.   end;  { UpStr }
  185.  
  186. ENd.
  187.  
  188.  
  189. {------------------ test program -----------------------}
  190.  
  191. {This will open the windows INI file WIN.INI and find data}
  192.  
  193. Program Test;
  194. Uses INIT;
  195.  
  196. Const
  197.  INIFILEStr = 'C:\WINDOWS\WIN.INI';
  198.  
  199. Var
  200.   ANyA : INI_TYP;
  201.   Result : Byte;
  202.  
  203. Begin
  204.  INITVALS(ANYA);
  205.  ANYA[1].Key := 'sCountry';
  206.  Result := ReadINI(INIFILEstr,ANYA);
  207.  If Result <> INI_FOK then
  208.   Begin
  209.    Writeln('');
  210.    Writeln('It seems that you are missing the file ',INIFILEStr);
  211.    Writeln('so I cannot detect your country.');
  212.    Writeln('');
  213.    readln;
  214.    Halt(1);
  215.   End;
  216.  Writeln('');
  217.  Writeln('It seems that you live in the ',ANYA[1].RESULT,'.');
  218.  Writeln('What a great place!');
  219.  Writeln('');
  220.  Readln;
  221. ENd.